package com.example.tao.smartcar.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.example.tao.smartcar.BaseActivity;
import com.example.tao.smartcar.R;
import com.example.tao.smartcar.utils.BluetoothService;

public class OperationSettingActivity extends BaseActivity implements View.OnClickListener {

    public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
    public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";

    private LinearLayout mTouchControl, mGravityControl;

    private String mDeviceAddress;

    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothDevice mDevice = null;
    private static BluetoothService mBluetoorhService = null;

    public static Handler mConnectedHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operation_setting);

        initView();


        final Intent intent = getIntent();
        mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);

        mConnectedHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == BluetoothService.CONNECTION_FAIL) {
                    //Toast.makeText(OperationSettingActivity.this, "蓝牙设备连接失败", Toast.LENGTH_LONG).show();
                    //mConnectStateImg.setImageResource(R.mipmap.bluetooth_not_connected);
                } else {
                    //mConnectStateImg.setImageResource(R.mipmap.bluetooth_connected);
                }

                super.handleMessage(msg);
            }
        };

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
        mBluetoorhService = new BluetoothService(this, mBluetoothAdapter);
        mBluetoorhService.connect(mDevice);
    }

    private void initView() {
        mTouchControl = (LinearLayout) this.findViewById(R.id.touch_control);
        mGravityControl = (LinearLayout) this.findViewById(R.id.gravity_control);

        mTouchControl.setOnClickListener(this);
        mGravityControl.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.touch_control :
                startActivity(new Intent(OperationSettingActivity.this, TouchControlActivity.class));
                break;
            case R.id.gravity_control :
                startActivity(new Intent(OperationSettingActivity.this, GravityControlActivity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mBluetoorhService != null) {
            mBluetoorhService.stop();
        }
        mConnectedHandler = null;
    }

    public static BluetoothService getBluetoorhService() {
        return mBluetoorhService;
    }
}
